<template>
  <div>
    <div class="header">
      <Breadcrumb></Breadcrumb>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="产品名称">
          <el-input
            v-model="formInline.name"
            @blur="blur"
            placeholder="产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="添加时间">
          <el-date-picker
            v-model="formInline.date"
            align="right"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="group">
        <el-button
          size="small"
          type="warning"
          icon="el-icon-plus"
          @click="toProductPage"
          >添加商品</el-button
        >
        <el-button
          size="small"
          type="danger"
          icon="el-icon-delete"
          @click="pldelates"
          >批量删除</el-button
        >
      </div>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select="selectHandle"
        @select-all="selectHandle"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="商品编号" width="120">
        </el-table-column>
        <el-table-column
          prop="title"
          label="商品名称"
          width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span
              style="color: blue; cursor: pointer"
              @click="handleLook(scope.$index, scope.row)"
              >{{ scope.row.title }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="price" label="商品价格" width="120">
        </el-table-column>
        <el-table-column prop="category" label="商品类目" width="120">
        </el-table-column>
        <el-table-column label="添加时间">
          <template slot-scope="scope">
            <span>{{
              moment(scope.row.create_time).format("YYYY-MM-DD HH:mm:ss")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="商品描述" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ removeHTMLTag(scope.row.descs) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination
          :total="total"
          :pageSize="pageSize"
          @currentPage="currentPage"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { projectList, delate, searchTable, plDelate } from "@/http/index.js";
import moment from "moment";
import { removeHTMLTag } from "@/utils/common.js";
import { mapMutations } from "vuex";
// 导入封装的分页组件
import Pagination from "@/components/pagination/index.vue";
export default {
  name: "GoodsList",
  components: {
    Pagination,
  },
  data() {
    return {
      formInline: {
        name: "",
        date: "",
      },
      tableData: [],
      total: 1,
      pageSize: 1,
      current: 1,
      ids: [],
    };
  },
  methods: {
    moment, //注册方法
    removeHTMLTag,
    ...mapMutations("product", ["changeRowData", "changeTitle"]),
    initList(page) {
      projectList({ page }).then((res) => {
        console.log(res, "table的数据");
        this.tableData = res.data.data;
        this.total = res.data.total;
        this.pageSize = res.data.pageSize;
      });
    },

    currentPage(val) {
      this.current = val;
      this.initList(this.current);
    },
    // 批量删除的操作]
    // 1.获取批量删除所选定的行的cid
    selectHandle(selection) {
      console.log(selection, "批量删除的列表");
      this.ids = selection.map((item) => {
        return item.id;
      });

      // console.log(this.ids, "删除列表的cid");
    },

    // 2,进行删除的操作
    pldelates() {
      let idStr = this.ids.join(",");
      // console.log(idStr, "哪里出了问题");
      //  plDelates({})
      this.$confirm("是否批量删除这些信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          plDelate({ ids: idStr })
            .then((res) => {
              if (res.data.status == 200) {
                //删除成功
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                let maxPage = Math.ceil(this.total / this.pageSize);
                let len = this.ids.length;
                if (maxPage == this.current) {
                  let num =
                    this.total % this.pageSize == 0
                      ? this.pageSize
                      : this.total % this.pageSize;
                  if (num == len) {
                    this.current = this.current - 1 > 0 ? this.current - 1 : 1;
                  }
                }
                this.initList(this.current);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 删除操作
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //删除这个数据---请求后台接口----同步数据库-----------------
          delate({ id }).then((res) => {
            console.log(res, "删除成功了没");
            if (res.data.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              if (this.total % this.pageSize == 1) {
                // alert("这里面我能进来");
                console.log(this.current, "你就是实话笨");

                this.current = this.current - 1 > 0 ? this.current - 1 : 1;
              }
              console.log(this.current, "这个的值有点奇怪");
              this.initList(this.current);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 查询操作
    onSubmit() {
      console.log(666);
      let search = this.formInline.name;
      if (this.formInline.name == "") retuen;
      searchTable({ search }).then((res) => {
        console.log(res, "搜索的内容");
        if (res.data.status === 200) {
          this.tableData = res.data.result;

          //  这里面是对于分页的处理’
          this.total = res.data.result.length;
          this.pageSize = res.data.result.length;
        } else {
          this.tableData = [];
          this.total = 1;
          this.pageSize = 1;
        }
      });
    },

    // 失去焦点之后的操作
    blur() {
      if (!this.formInline.name) {
        this.initList();
      }
    },
    // 添加商品的操作
    toProductPage() {
      this.$router.push("/GoodsControl/addProduct");
      this.changeTitle("添加");
    },
    // 详情商品页面
    handleLook(index, row) {
      this.changeTitle("详情");
      this.changeRowData(row);
      this.$router.push("/GoodsControl/addProduct");
    },
    // 编辑商品页面
    handleEdit(index, row) {
      this.changeTitle("编辑");
      this.changeRowData(row);
      this.$router.push("/GoodsControl/addProduct");
    },
  },
  created() {},
  mounted() {
    this.initList();
  },
};
</script>
<style lang="scss" scoped>
.header {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  //   align-items: center;
  padding: 10px 5px;
  margin: 20px;
  background-color: #fff;
  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}
.table {
  background-color: #fff;

  ::v-deep .active-header {
    color: #333;
    text-align: center;
  }

  ::v-deep .table-center {
    text-align: center;
  }
  .pagination {
    height: 60px;
    background-color: #fff;
    padding: 15px 50px;
  }
}
</style>
